package tgc.edu.exam.custom.dict.web.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.UUID;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import tgc.edu.exam.custom.DataGridParam;
import tgc.edu.exam.custom.DataGridUtils;
import tgc.edu.exam.custom.LayUiResult;
import tgc.edu.exam.custom.dict.entity.TbClassType;
import tgc.edu.exam.custom.dict.service.TbClassTypeService;
import tgc.edu.exam.custom.dict.web.form.TbClassStateForm;
import tgc.edu.exam.custom.dict.web.form.TbClassTypeForm;
import tgc.edu.exam.security.UserUtils;

@Controller
@RequestMapping(value="/dict/tbClassType")
public class TbClassTypeController {
	@Autowired
	private TbClassTypeService tbClassTypeDAO;
	@Autowired
	private UserUtils userUtils;
	
	@RequestMapping(value= "/manage")
	public String main1(DataGridParam param, ModelMap map) {
		map.put("param", param);
		return "dict/tbClassType/manage";
	}
	
	@RequestMapping(value="/edit")
	public String add(String id, ModelMap map) {
		TbClassType tbClassType = new TbClassType();
		if(id != null || "".equals(id)) {
			tbClassType = tbClassTypeDAO.findById(UUID.fromString(id));
		}
		map.put("model", tbClassType);
		return "dict/tbClassType/edit";
	}
	
	@RequestMapping(value="/save")
	@ResponseBody
	public Object save(TbClassTypeForm form) {
		TbClassType model = new TbClassType();
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		String id=form.getId();
		if(StringUtils.hasText(id)) {
			model=tbClassTypeDAO.findById(UUID.fromString(id));
			model.setUpdateDate(sdf.format(new Date()));
			model.setUpdateName(userUtils.getName());
		}else {
			model.setCreateDate(sdf.format(new Date()));
			model.setCreateName(userUtils.getName());
		}
		BeanUtils.copyProperties(form, model,"id");
		tbClassTypeDAO.save(model);
		return LayUiResult.getResult();
	}
	
	@RequestMapping(value="/delete")
	@ResponseBody
	public Object delete(String id) {
		tbClassTypeDAO.deleteById(UUID.fromString(id));
		return LayUiResult.getResult();
	}
	
	@RequestMapping(value="/page")
	@ResponseBody
	public HashMap<String, Object> data(ModelMap map, DataGridParam param, TbClassTypeForm form) {
		Sort sort=Sort.by("createDate");
		Pageable pabeable = param.getPageable(sort);
		Specification<TbClassType> spec = buildSpec(form);
		Page<TbClassType> page = tbClassTypeDAO.findAll(spec, pabeable);
		return DataGridUtils.buildResult(page);
	}

	private Specification<TbClassType> buildSpec(TbClassTypeForm form) {
		Specification<TbClassType> specification = new Specification<TbClassType>() {

			private static final long serialVersionUID = 1L;
			
			@Override
			public Predicate toPredicate(Root<TbClassType> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				HashSet<Predicate> rules=new HashSet<>();
				if(StringUtils.hasText(form.getCode())) {
					Predicate name = cb.like(root.get("code"), "%"+form.getCode()+"%");
					rules.add(name);
				}
				if(StringUtils.hasText(form.getType())) {
					Predicate name = cb.like(root.get("type"), "%"+form.getType()+"%");
					rules.add(name);
				}
				return cb.and(rules.toArray(new Predicate[rules.size()]));
			}

		};
		return specification;
	}
	
	@RequestMapping(value="/search")
	public void search(TbClassStateForm form,ModelMap map) {
		map.put("model",form);
	}
}
